<?php
namespace app\cash\controller;
use think\Controller;
use think\Db;
use app\cash\model\OrderDetails;
use app\cash\model\StoreGoodsLog;
class Countprice20180910 extends Controller{
    /**
     * @Author lignyun
     * @Desc    检查登录
     * 2018-04-09
     */
    public function _initialize(){
        if(!session('cash_user')) {
            $this->redirect('@cash/login/index');
        }
    }

    /**
     * @Author  lingyun
     * @Desc    根据商品属性计算商品价格
     * 2018-06-06
     */
    public function index(){
        $goods_id = input('goods_id');                      //商品id
        $spec_ids = rtrim(input('spec_details'),',');                  //商品属性组合
        $store_id = session('cash_user')['store_id'];       //门店id
        $mobile = input('mobile');                          //手机号
        $store = db('store')->where('id',$store_id)->find();        //门店信息

        if(!empty($mobile)){

            $member = db('user')->where('mobile',$mobile)->find();
            if(empty($member)){
                $member = db('user')->where('pay_code',$mobile)->find();
            }
        }else{
            $member = array();
        }

        $goods = db('store_goods_stock')->alias('a')->join('ys_goods_spec_detail b','a.goods_spec_id=b.id','LEFT')->join('ys_goods c','a.goods_id=c.id','LEFT')->where(['a.store_id'=>$store_id,'a.specs'=>$spec_ids,'a.goods_id'=>$goods_id])->field('a.id,a.discount,a.goods_spec_id,a.goods_id,a.stock,a.onsale,a.trim_price,a.is_deleted,a.is_diannei,b.price,b.member_price,c.name,b.title,c.member_level_id')->find();
        if(empty($goods)){
            $this->error('找不到该商品信息，请选择其它商品','');
        }else if($goods['onsale'] == 0){
            $this->error('该商品已下架，请选择其它商品','');
        }else if($goods['is_deleted'] == 1){
            $this->error('该商品已下架，请选择其它商品','');
        }else if($goods['stock'] < 1){
            $this->error('该商品库存不足，请选择其它商品','');
        }

        if(!empty($member)){        //计算会员价
            if($store['price_status'] == 1 && $goods['trim_price'] != 0){
//                $goods['final_amount'] = $goods['trim_price'];              //使用商品调价
                $goods['final_amount'] = $goods['price'];              //使用商品调价
            }else if($member['level_id'] >= $goods['member_level_id']){
                $goods['final_amount'] = $goods['member_price'];
            }else{
                $goods['final_amount'] = $goods['price'];
            }
        }else{
            if($store['price_status'] == 1 && $goods['trim_price'] != 0){
//                $goods['final_amount'] = $goods['trim_price'];
                $goods['final_amount'] = $goods['price'];
            }else{
                $goods['final_amount'] = $goods['price'];
            }
        }

        if(strlen(trim($goods['name'])) > 8){
            $goods['name'] = mb_substr($goods['name'],0,7,"utf-8").'...';
        }
        if(!empty($goods['title'])) {
//            if (strlen(trim($goods['title'])) > 10) {
//                $goods['title'] = mb_substr($goods['title'], 0, 10, "utf-8") . '...';
//            }
        }else{
            $goods['title'] = '无';
        }
        $goods['discount'] = empty($goods['discount'])?0:$goods['discount'];
        $data = [
            'id'=>$goods['id'],
            'goods_spec_id'=>$goods['goods_spec_id'],
            'goods_id'=>$goods['goods_id'],
            'stock'=>$goods['stock'],
            'is_diannei'=>$goods['is_diannei'],
            'title'=>$goods['title'],
            'name'=>$goods['name'],
            'final_amount'=>$goods['final_amount'],
            'discount'=>$goods['discount']
        ];

        $this->success('查询成功','',$data);
    }

    /**
     * @Author  lingyun
     * @Desc    获取优惠券
     * 2018-06-06
     */
    public function get_coupon(){
        $post = input('post.');
        $store_id = session('cash_user')['store_id'];
        $store = db('store')->where('id',$store_id)->field('id,price_status,is_jiameng')->find();
        if(!empty($post['mobile'])){
            $member = db('user')->where('mobile',$post['mobile'])->field('uid,level_id,user_money')->find();
            if(empty($member)){
                $member = db('user')->where('pay_code',$post['mobile'])->field('uid,level_id,user_money')->find();
            }

            if(empty($member)){
                $this->error('未找到会员信息，请检查手机号或会员卡号是否正确','');
            }
        }else{
            $this->error('请输入会员手机号或会员卡号','');
        }

        $goods = [];
        $cate = [];             //商品分类
        $coupon_cate = [];      //优惠券分类
        foreach($post['goods_id'] as $k => $v){
            $map['a.goods_id'] = ['eq',$v];
            $map['a.goods_spec_id'] = ['eq',$post['goods_spec_id'][$k]];
//            $map['a.is_deleted'] = ['eq',0];
//            $map['a.onsale'] = ['eq',1];
            $result = db('store_goods_stock')->alias('a')->join('ys_goods b','a.goods_id=b.id','LEFT')->join('ys_goods_spec_detail c','a.goods_spec_id=c.id','LEFT')->where($map)->field('a.stock,a.onsale,is_diannei,a.is_deleted,b.name,c.title,c.price,c.member_price,a.trim_price,b.member_level_id,b.cat_id')->find();

            if(empty($result)){
                $this->error('找不到商品信息，请检查商品','');
            }else if($result['onsale'] == 0 || $result['is_deleted'] == 1){
                $this->error($result['name'].' - '.$result['title'].'已下架，请重新选择','');
            }else if($result['stock'] < $post['goods_num'][$k]){
                $this->error($result['name'].' - '.$result['title'].'库存不足，请重新选择');
            }

            if($member['level_id'] >= $result['member_level_id']){
                $result['price'] = $result['member_price'];
            }else if($store['price_status'] == 1 && $result['is_diannei'] == 1 && !empty($result['trim_price'])){
                $result['price'] = $result['member_price'];
            }

//            if($store['price_status'] == 1 && $result['is_diannei'] == 1 && !empty($result['trim_price'])){            //店内商品并且允许调价
//                $result['price'] = $result['trim_price'];     //使用调价
//            }else if($member['level_id']>=$result['member_level_id']){
//                $result['price'] = $result['member_price'];
//            }
            $result['final_amount'] = $result['price']*$post['goods_num'][$k];    //最终支付价

            if(isset($cate[$result['cat_id']])){
                $cate[$result['cat_id']] = $cate[$result['cat_id']]+$result['final_amount'];
            }else{
                $cate[$result['cat_id']] = $result['final_amount'];
            }

            array_push($coupon_cate,$result['cat_id']);         //优惠券类型
        }

//        $coupon_list = db('user_ycard')->alias('a')->join('ys_store_coupon b','a.coupon_id=b.id','LEFT')->join('ys_goods_category c','a.cat_id=c.id','LEFT')->where(['a.type'=>['neq',2],'a.uid'=>$member['uid'],'a.use_start_time'=>['lt',time()],'a.use_end_time'=>['gt',time()],'a.is_used'=>0,'a.store_id'=>$store_id,'a.status'=>1])->field('b.name,a.coupon_id,a.reduce,b.cat_id,FROM_UNIXTIME(a.use_start_time,"%Y-%m-%d") as start_time,FROM_UNIXTIME(a.use_end_time,"%Y-%m-%d") as end_time,a.morethan,b.name,c.title')->select();

        $map1['a.store_id'] = ['eq',$store_id];
        $map1['a.type'] = ['neq',2];
        $map1['a.cat_id'] = ['in',$coupon_cate];
        $map1['a.is_used'] = ['eq',0];
        $map1['a.uid'] = ['eq',$member['uid']];
        $map1['a.status'] = ['eq',1];
        $map1['a.use_start_time'] = ['lt',time()];
        $map1['a.use_end_time'] = ['gt',time()];
        $coupon_list = db('user_ycard')->alias('a')->join('ys_store_coupon b','a.coupon_id=b.id','LEFT')->where($map1)->field('a.*,FROM_UNIXTIME(a.use_start_time,"%Y-%m-%d") as start_time,FROM_UNIXTIME(a.use_end_time,"%Y-%m-%d") as end_time,b.name')->select();        //优惠券

        foreach($coupon_list as $k => $v){
            if($coupon_list[$k]['cat_id'] == 0){
                continue;
            }
            if(!isset($cate[$v['cat_id']])){
                unset($coupon_list[$k]);
            }else if($v['morethan'] > $cate[$v['cat_id']]){
                unset($coupon_list[$k]);
            }
        }

        if(count($coupon_list) == 0){
            $this->error('没有可用优惠券','');
        }else{
            $this->success('保存成功','',$coupon_list);
        }
    }

    /**
     * @Author  lingyun
     * @Desc    查询优惠券 - 新
     * 2018-09=08
     */
    public function get_coupon_new(){
        $post = input('post.');
        $store_id = session('cash_user')['store_id'];   //门店id
        $map['a.id'] = ['in',$post['goods_spec_id']];
        $map['b.store_id'] = ['eq',$store_id];      //门店id

        if(!empty($post['mobile'])){
            $member = db('user')->where('mobile',$post['mobile'])->field('uid,level_id,user_money')->find();
            if(empty($member)){
                $member = db('user')->where('pay_code',$post['mobile'])->field('uid,level_id,user_money')->find();
            }

            if(empty($member)){
                $this->error('未找到会员信息，请检查手机号或会员卡号是否正确','');
            }
        }else{
            $this->error('请输入会员手机号或会员卡号','');
        }

        $goods = db('goods_spec_detail')->alias('a')->join('ys_goods_stock b','a.goods_spec_id=b.id','LEFT')->join('ys_goods c','a.goods_id=c.id','LEFT')->where($map)->field('a.id,b.goods_spec_id,c.cat_id,a.price,b.member_price,c.trim_price,b.member_level_id')->select();         //商品信息

    }


}